package org.dromara.crawler.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.crawler.domain.KnowledgeTemplate;
import org.dromara.crawler.mapper.KnowledgeTemplateMapper;
import org.dromara.crawler.service.KnowledgeTemplateService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 模版管理Service服务
 *
 * @author shup
 * @date 2025-6-18
 */
@Service
@RequiredArgsConstructor
public class KnowledgeTemplateServiceImpl extends ServiceImpl<KnowledgeTemplateMapper, KnowledgeTemplate> implements KnowledgeTemplateService {


    @Override
    public IPage<KnowledgeTemplate> queryByPage(KnowledgeTemplate knowledgeTemplate, PageQuery pageQuery) {
        QueryWrapper<KnowledgeTemplate> wrapper = new QueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(knowledgeTemplate.getTemplateName()), "kt.template_name", knowledgeTemplate.getTemplateName());
        wrapper.eq(StringUtils.isNotBlank(knowledgeTemplate.getTemplateType()), "kt.template_type", knowledgeTemplate.getTemplateType());
        wrapper.eq(StringUtils.isNotBlank(knowledgeTemplate.getTemplateSecondaryType()), "kt.template_secondary_type", knowledgeTemplate.getTemplateSecondaryType());
        wrapper.orderByDesc("kt.id");
        return baseMapper.queryByPage(pageQuery.build(), wrapper);
    }

    @Override
    public List<KnowledgeTemplate> queryList(KnowledgeTemplate knowledgeTemplate) {
        QueryWrapper<KnowledgeTemplate> wrapper = new QueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(knowledgeTemplate.getTemplateName()), "kt.template_name", knowledgeTemplate.getTemplateName());
        wrapper.eq(StringUtils.isNotBlank(knowledgeTemplate.getTemplateType()), "kt.template_type", knowledgeTemplate.getTemplateType());
        wrapper.eq(StringUtils.isNotBlank(knowledgeTemplate.getTemplateSecondaryType()), "kt.template_secondary_type", knowledgeTemplate.getTemplateSecondaryType());
        wrapper.eq(ObjectUtil.isNotEmpty(knowledgeTemplate.getStatus()), "kt.status", knowledgeTemplate.getStatus());
        wrapper.orderByDesc("kt.id");
        return baseMapper.queryList(wrapper);
    }
}
